<template>
  <div class="login_container">
    <!-- login       -->
    <div class="login_box">
      <div class="avtar_box">
        <!-- 头像 -->
        <img src="../assets/logo.png" alt>
      </div>
      <!-- 表单 -->
      <el-form
        ref="loginref"
        :model="loginform"
        :rules="loginrules"
        label-width="80px"
        class="form_box"
      >
        <el-form-item label="用户名" prop="username">
          <el-input placeholder="用户名" prefix-icon="iconfont icon-user" v-model="loginform.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            placeholder="密码"
            prefix-icon="iconfont icon-3702mima"
            v-model="loginform.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <!-- 按钮 -->
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="success" @click="resetlogin">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginform: {
        username: "admin",
        password: "123456"
      },
      loginrules: {
        // elementui表单验证里的固定写法
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "请输入3-10个字", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 10, message: "请输入3-10个字", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //   点击重置表单
    resetlogin() {
      //   console.log(this);
      //   固定写法
      this.$refs.loginref.resetFields();
    },
    login() {
      this.$refs.loginref.validate(async valid => {
        //   console.log(valid);通过true，false错误
        if (!valid) return; //错了
        let { data: result } = await this.$http.post("login", this.loginform); //解构赋值 result是取得小名
        // console.log(result);
        if (result.meta.status != 200) return this.$message.error("登录失败");
        this.$message.success("登录成功");
        // 1 存token
        //  sessionStorage.setItem(名字,值)存
        //  sessionStorage.getItem(名字)取
        //  sessionStorage.clearItem()删
        sessionStorage.setItem("token", result.data.token);
        // 2跳转到home页面
        this.$router.push("/home");
      });
    }
  }
};
</script>
<style lang="less" scoped>
// 写样式lang选择你要用的css语法：css，less，scss ，stylus
// scoped样式就只可以局部访问
.login_container {
  height: 100%;
  background: #2b4b6b;
  .login_box {
    height: 320px;
    width: 500px;
    background: white;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avtar_box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      background: #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: #eee;
      }
    }
    .form_box {
      margin-top: 110px;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
      .el-form-item {
        padding: 0px 15px;
      }
    }
  }
}
</style>
